home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Archive Magazine CD 1995
/
Archive Magazine CD 1995.iso
/
discs
/
pipeline
/
abacus
/
p_line
/
Custom05
/
c_nCr
< prev
next >
Wrap
Text File
|
1995-04-09
|
4KB
|
284 lines
%OP%VS4.13 (28-Apr-92), Gerald L Fitton, R4000 5966 9904 9938
%OP%TNN
%OP%WRN
%OP%DP0
%OP%TH3
%OP%IRN
%OP%PL0
%OP%HM0
%OP%FM0
%OP%BM0
%OP%LM4
%OP%PT1
%OP%PDPipeLine
%OP%WC2,1278,116,1672,0,77,0,45
%OP%NDiteration_number,B58
%OP%NDloop_counter,1001
%OP%NDcurrent_value,B13
%OP%NDloop_counter_2,1001
%OP%NDtemporary_answer,B56
%OP%NDanswer,B56
%OP%NDiteration_number_1,B57
%OP%NDiteration_number_2,B58
%OP%NDinteration_number_3,B59
%OP%NDiteration_number_3,B59
%OP%NDr_on_entry,B57
%OP%NDr_on_exit,B58
%OP%NDentry_or_exit,B59
%OP%FR0,2
%CO:A,54,72%Comments and Commands
------------------------------------------------------------------------
The custom function "binomial_1" calculates the value of nCr(n,r)
It does so by using a For - Next Loop to multiply the named variable
"current_value" by n/r, then by (n-1)/(r-1), etc
%V%%L%function("binomial_1","n:number","r:number")
Declare the name of the local variable and the slot it uses
%V%%L%set_name("current_value",B13)
Initialise value of named variable
%V%%L%set_value(current_value,1)
Now execute the For - Next loop r times
%V%%L%for("loop_counter",0,@r-1)
Slow down the loop so we can see it operate (look at B13)
%V%%L% for("loop_counter_2",1,1000)
%V%%L% next
The next line does the 'work'
%V%%L% set_value(current_value,current_value*(@n-loop_counter)/(@r-loop_counter))
%V%%L%next
Return the result
%V%%L%result(current_value)
The custom function "binomial_2" calculates the value of nCr(n,r).
It does so by using a recursive call to the function "binomial_2".
Each call to the custom function is executed only down to the point
where it becomes recursive. I call this 'entering' because you can
imagine that each time you reach the point of recursion you enter a
gateway to the next recursion. You can see this happening if you
observe the value of the local variable "r_on_entry" as the early part
of the function is executed for reducing values of r.
When the value of r = 1 we have to stop the recursive procedure and
'unwind' it by using the 'exit' part of the custom function. You can
see this happening by observing the value of the local variable
"r_on_exit".
Also you will see that the local variable "entry_or_exit" takes
appropriate values to show which part of the loop is being executed.
%V%%L%function("binomial_2","n:number","r:number")
Declare the 'names' of the variables and the slots they will use
%V%%L%set_name("answer",B56)
%V%%L%set_name("r_on_entry",B57)
%V%%L%set_name("r_on_exit",B58)
%V%%L%set_name("entry_or_exit",B59)
Initialise the local variables
%V%%L%set_value(answer,0)
%V%%L%set_value(r_on_entry,0)
%V%%L%set_value(r_on_exit,0)
%V%%L%set_value(entry_or_exit,"")
Show which value of r we are using during entry to recursion
%V%%L%set_value(entry_or_exit,"Entry")
%V%%L%set_value(r_on_entry,@r)
Slow down the operation so we can see it work
%V%%L%for("loop_counter",1,1000)
%V%%L%next
The next line contains the recursive call to the "binomial_2" function
%V%%L%set_value(answer,if(@r>1,@n/@r*binomial_2(@n-1,@r-1),@n))
Show which iteration is being executed during exit of recursion
%V%%L%set_value(entry_or_exit,"Exit")
%V%%L%set_value(r_on_exit,@r)
Slow down the operation so we can see it work
%V%%L%for("loop_counter",1,1000)
%V%%L%next
Return the result
%V%%L%result(answer)
%CO:B,12,0%Value
%V%%R%120
%V%%R%15180
%V%%R%1
%V%%R%3
%V%%R%"Exit"
%CO:C,6,0%